# Copyright (C) 2006  Apptility LLC  http://www.apptility.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

module AjubySoapBox
  require 'soap/marshal'
  require 'soap/wsdlDriver'
  require 'soap/header/simplehandler'

   def self.connect(wsdl_url, debug)
     factory = SOAP::WSDLDriverFactory.new(wsdl_url)
     service = factory.create_rpc_driver
     if debug=='true'
       service.wiredump_dev=STDOUT
     end
     service
  end
  
  def self.connect_w_header(wsdl_url, server_url, debug, header_handler)
     factory = SOAP::WSDLDriverFactory.new(wsdl_url)
     service = factory.create_rpc_driver
     service.endpoint_url = server_url if server_url!=nil
     
     if debug=='true'
       service.wiredump_dev=STDOUT
     end
     service.headerhandler << header_handler
     return service
  end
  
  def self.xml_to_obj (sample_xml_file)
    xml = File.open(sample_xml_file, "r" )
    env = SOAP::Processor.unmarshal(xml)
    body = env.body
    root = body.root_node
    obj = SOAP::Mapping.soap2obj(root)
    obj
  end
 
  
  # Authorization Header Handler class, Generic class can be utilized by other interfaces/utility classes to 
  # Build custom SOAP Headers and attach it to SOAP messages.
  
  class SOAPHeaderHandler < SOAP::Header::SimpleHandler     
     # Default name of the header, if not specified
     @@HEADER_NAME = 'SessionHeader'
     @@HEADER_NAMEASPACE = 'urn:ajuby.openapp.org'

     def initialize(header_name, header_namespace, options) 
       @out_bound_hash = Hash.new()
       # Set the header namespace and the name
       @@HEADER_NAMEASPACE = header_namespace if header_namespace!=nil
       @@HEADER_NAME = header_name if header_name!=nil
       super(XSD::QName.new(@@HEADER_NAMEASPACE, @@HEADER_NAME))

       options.each_key {|key| 
         @node_values_array = options[key]
         # above value returns an array, later it can be extended as hash to make it scalable.
         # for now, we expect only node value and namespace in the array
         node_value = @node_values_array[0]
         node_namespace = @node_values_array[1]
         @node_element = XSD::QName.new(node_namespace, key)
         @out_bound_hash[@node_element] = node_value
       }
       #defaults
       @mustunderstand=false
     end
     
     def on_simple_outbound
      @out_bound_hash
     end   
  end
  
  
end